<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            padding-bottom: 800px;
        }
        .switch-box{
            font-size: 12px;
            user-select: none;
        }
        .switch-box>span{
            font-size: 1.2em;
            vertical-align: middle;
            /* font-weight: bold; */
        }
        .switch-box>span.active{
            color: #409eff;
        }
        .switch-btn{
            vertical-align: middle;
            margin: 0px 0.5em;
            display: inline-block;
            /* 单位采用em */
            width: 4em;
            height: 2em;
            border-radius: 2em;
            cursor: pointer;
            background-color: #dcdfe6;
            box-shadow: 0 0 0.3em #999 inset;
            transition: all 400ms ease;
        }
        .switch-btn::after{
            content: "";
            display: inline-block;
            width: 2em;
            height: 2em;
            background-color: white;
            border-radius: 50%;
            transform: translate(0%,0%) scale(0.8);
            box-shadow: 0 0 0.3em #999;
            transition: all 400ms ease;
        }
        .switch-box input[type="checkbox"]{
            display: none;
        }
        .switch-box input[type="checkbox"]:checked + .switch-btn{
            background-color: #409eff;
            box-shadow: 0 0 0.3em #2c6fb1 inset;
        }
        .switch-box input[type="checkbox"]:checked + .switch-btn::after{
            transform: translate(100%,0%) scale(0.8);
        }
        .rights-list{
            width: 200px;
        }
        .rights-list > li{
            padding: 4px 0px;
            display: flex;
            flex-flow: row nowrap;
            justify-content: space-between;
        }
    </style>
</head>
<body>
    <div id="app">
        <pre>{{ rights }}</pre>
        <ul class="rights-list">
            <!-- <li>
                <span>允许访问通讯录</span>
                <switch-btn></switch-btn>
            </li>
            <li>
                <span>允许访问相册</span>
                <switch-btn></switch-btn>
            </li>
            <li>
                <span>允许访问麦克风</span>
                <switch-btn></switch-btn>
            </li>
            <li>
                <span>允许访问摄像头</span>
                <switch-btn></switch-btn>
            </li> -->
            <li v-for="(item,i) in rights">
                <span>{{ item.info }}</span>
                <switch-btn :flag="item.state" @update:flag="setFlag($event,i)" ></switch-btn>
            </li>
        </ul>
    </div>
    <!-- 功能组件(ui组件) -->
    <script type="text/x-template" id="switchBtn">
        <label class="switch-box">
            <input type="checkbox" v-model="cflag">
            <div class="switch-btn"></div>
        </label>
    </script>
    <script type="module"> 
        import { createApp } from "../../assets/vue3/vue.esm-browser.js";
        const app = createApp({
            data(){
                return {
                    rights:[
                        { info:"允许访问通讯录",state:false },
                        { info:"允许访问相册",state:true },
                        { info:"允许访问麦克风",state:false },
                        { info:"允许访问摄像头",state:true }
                    ]
                }
            },
            methods: {
                setFlag(e,i) {
                    this.rights[i].state = e;
                }
            },
        });
        
        app.component("SwitchBtn",{
            template:"#switchBtn",
            // data() {
            //     return {
            //         flag: false
            //     }
            // },
            emits:["update:flag"],
            props:{
                flag:Boolean
            },
            computed: {
                cflag:{
                    get(){
                        return this.flag
                    },
                    set(nv){
                        this.$emit("update:flag",nv)
                    }
                }
            },
        })
        
        app.mount("#app")
    </script>
</body>
</html>